package com.example.xiaxiamusicplayer.adapler;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.xiaxiamusicplayer.MusicDetailActivity;
import com.example.xiaxiamusicplayer.R;
import com.example.xiaxiamusicplayer.bean.Mp3;
import com.example.xiaxiamusicplayer.utils.XiaXiaPlayer;

import java.util.List;

public class Mp3ListAdapter extends RecyclerView.Adapter<Mp3ListAdapter.Mp3ListHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<Mp3> mData;

    public Mp3ListAdapter(Context context, List<Mp3> data) {
        mContext = context;
        mData = data;
        mInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public Mp3ListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Mp3ListHolder(mInflater.inflate(R.layout.item_mp3_list, parent, false));
    }

    @NonNull

    @Override
    public void onBindViewHolder(@NonNull Mp3ListHolder holder, int position) {
        Mp3 mp3 = mData.get(position);
        holder.tvName.setText(mp3.name);
        holder.tvTime.setText(mp3.time);
        holder.tvPosition.setText((position + 1) + ".");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XiaXiaPlayer.getInstance().startPlay(mp3);
                mContext.startActivity(new Intent(mContext, MusicDetailActivity.class));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public static class Mp3ListHolder extends RecyclerView.ViewHolder {
        private View itemView;
        private TextView tvName;
        private TextView tvTime;
        private TextView tvPosition;

        public Mp3ListHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView = itemView;
            tvName = itemView.findViewById(R.id.tv_name);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvPosition = itemView.findViewById(R.id.tv_position);
        }
    }
}
